// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import ItemFrame from "../item/itemFrame";
import {getReward} from "../config/config";
import {globalConfig} from "../config/globalConfig";
import {COMMON_CID} from "../web/model/UserModel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class LuckyResult extends cc.Component {

  @property(cc.Node)
  itemFrame: cc.Node = null
  @property([cc.Node])
  itemFrames: [cc.Node] = []
  @property([cc.Node])
  twoFrames: [cc.Node] = []
  @property(cc.Node)
  tenResult: cc.Node = null
  @property(cc.Node)
  singleResult: cc.Node = null

  setResultIndex(index: [number], isDiamond = true) {
    this.tenResult.active = false
    this.itemFrame.active = false
    this.singleResult.active = false
    if (!isDiamond) {
      this.itemFrame.active = true
      const config: any = globalConfig.turntable[index[0] - 1]
      const rewardConfig: any = getReward(config.reward)
      this.itemFrame.getComponent(ItemFrame).setItemData(rewardConfig.itemid, rewardConfig.number, rewardConfig.type)
    } else if (index.length === 1) {
      this.singleResult.active = true
      const config: any = globalConfig.turntable[index[0] - 1]
      const rewardConfig: any = getReward(config.reward)
      this.twoFrames[0].getComponent(ItemFrame).setItemData(rewardConfig.itemid, rewardConfig.number, rewardConfig.type)
      this.twoFrames[1].getComponent(ItemFrame).setItemData(COMMON_CID.PIECE_POWERFUL, 1)
    } else {
      this.tenResult.active = true
      index.forEach((value, i) => {
        value = value - 1
        const config: any = globalConfig.turntable[value]
        const rewardConfig: any = getReward(config.reward)
        this.itemFrames[i].getComponent(ItemFrame).setItemData(rewardConfig.itemid, rewardConfig.number, rewardConfig.type)
      })
      this.itemFrames[this.itemFrames.length - 1].getComponent(ItemFrame).setItemData(COMMON_CID.PIECE_POWERFUL, 10)
    }
  }

  start() {

  }

  onClose() {
    this.node.active = false
  }

}
